package com.group11.invite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group11.common.pojo.Enterprise;
import com.group11.common.pojo.ResponseResult;
import com.group11.common.pojo.Resume;
import com.group11.invite.domain.Invite;
import com.group11.invite.dto.InviteDto;
import com.group11.invite.mapper.InviteMapper;
import com.group11.invite.mapper.JobSendMapper;
import com.group11.invite.service.InviteService;
import com.group11.invite.service.JobService;
import com.group11.invite.service.ResumeService;
import com.group11.invite.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class InviteServiceImpl extends ServiceImpl<InviteMapper, Invite> implements InviteService {

    @Autowired
    private InviteMapper inviteMapper;

    @Autowired
    private ResumeService resumeService;

    public List<Integer> getUserResumeIds(int userId) {
        ResponseResult<List<Resume>> listResponseResult = resumeService.listByUserId(userId);
        if (listResponseResult.getCode() != 200) {
            return null;
        }

        List<Resume> resumes = listResponseResult.getData();
        if (resumes == null || resumes.isEmpty()) {
            return null;
        }

        ArrayList<Integer> resume_ids = new ArrayList<>();
        resumes.forEach(resume -> {
            resume_ids.add(resume.getId());
        });
        return resume_ids;
    }


    @Override
    public boolean addInvite(Invite invite) {
        invite.setInviteTime(LocalDateTime.now().toString());
        int insert = inviteMapper.insert(invite);
        return insert > 0;
    }

    @Override
    public List<Invite> getInviteByUserId(int id) {

        List<Integer> userResumeIds = getUserResumeIds(id);

        QueryWrapper<Invite> inviteQueryWrapper = new QueryWrapper<>();
        inviteQueryWrapper.in("resume_id", userResumeIds);
        return inviteMapper.selectList(inviteQueryWrapper);
    }


    @Override
    public boolean reply(InviteDto inviteDto) {
        UpdateWrapper<Invite> inviteUpdateWrapper = new UpdateWrapper<>();
        inviteUpdateWrapper.eq("id",inviteDto.getId()).set("reply",inviteDto.getReply());
        return inviteMapper.update(inviteUpdateWrapper) > 0;

    }

    @Override
    public Invite ifInvite(int job_id, int resume_id) {
        QueryWrapper<Invite> inviteQueryWrapper = new QueryWrapper<>();
        inviteQueryWrapper
                .eq("resume_Id",resume_id)
                .eq("job_id",job_id);
        return inviteMapper.selectOne(inviteQueryWrapper);
    }
}
